/*
"JSFXGEN" - Connection.as
Modular Integrated Development Environment for JesuSonic
Project page: http://jsfxgen.googlecode.com

Copyright 2009 Lubomir I. Ivanov

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package neolit123.jsfxgen 
{
  import flash.display.Sprite;
  import flash.display.Graphics;
  import flash.events.MouseEvent;
  // =========================================================
  public final class Connection extends Sprite
  {
    private var line:Sprite;
    public var src:Object;
    public var dest:Object;
    private var basesrcx:Number;
    private var basesrcy:Number;
    private var basedestx:Number;
    private var basedesty:Number;
    private var oldsrcx:Number;
    private var oldsrcy:Number;
    private var olddestx:Number;
    private var olddesty:Number;
    private var str:String;
    public var id:int;
    // =========================================================
    // [ create connection
    // =========================================================
    public function Connection(setsrc:Object, setdest:Object, idx:int)
    {
      // @ set local src, dest
      id = idx;
      src = setsrc;
      dest = setdest;
      // @ lock desination
      dest.active = false;
      // @ create line
      line = new Sprite();
      addChild(line);
      line.buttonMode = true;
      // @ connection text
      str = src.name+" - "+dest.name;
      // @ tooltip / debug
      str = str.split("in:").join("").split("out:").join("");
      Main.tt.add(line,str);
      Main.debug.df("Added connection: "+str);
      // @ mouse actions
      line.addEventListener(MouseEvent.MOUSE_DOWN, md);
      function md(e:MouseEvent = null):void {
        remove();
      }
      line.addEventListener(MouseEvent.MOUSE_OVER, mover);
      function mover(e:MouseEvent = null):void {
        dest.alpha = src.alpha = alpha = 0.3;
      }
      line.addEventListener(MouseEvent.MOUSE_OUT, mout);
      function mout(e:MouseEvent = null):void {
        dest.alpha = src.alpha = alpha = 1;
      }
      redraw();
    }
    // =========================================================
    // [ redraw connection
    // =========================================================
    public function redraw():void
    {
      // @ x,y vars
      var srcx:Number,srcy:Number,destx:Number,desty:Number;
      // @ get src and dest object
      var srctmp:Object = src.parent.parent.parent.parent;
      var desttmp:Object = dest.parent.parent.parent.parent;
      // @ set base x,y
        basesrcx = src.parent.x+5;
        basesrcy = src.parent.parent.y+src.y+5;
        basedestx = dest.parent.x+5;
        basedesty = dest.parent.parent.y+dest.y+5;
        // @ set src x,y
      if (src.parent.parent.visible)
      {
         srcx = srctmp.x+basesrcx;
         srcy = srctmp.y+basesrcy;
      } 
      else
      {
         srcx = srctmp.x+src.parent.x;
         srcy = srctmp.y+10;
      }        
      // @ set dest x,y
      if (dest.parent.parent.visible)
      {
         destx = desttmp.x+basedestx;
         desty = desttmp.y+basedesty;
      }
      else
      { 
         destx = desttmp.x+dest.parent.x;
         desty = desttmp.y+10;
      }
      // @ redraw only if changed
      if(oldsrcx != srcx || oldsrcy != srcy 
      || olddestx != destx || olddesty != desty)
      {
        // @ redraw
        line.graphics.clear();
        line.graphics.lineStyle(3,0x000000,1);
        line.graphics.moveTo(srcx,srcy);
        line.graphics.lineTo(destx,desty);
        // @ set old
        oldsrcx = srcx;
        oldsrcy = srcy;
        olddestx = destx;
        olddesty = desty;
      }
    }
    // =========================================================
    // [ remove connection
    // =========================================================
    public function remove():void
    {  
      dest.active = true;       
      Main.lines.removeChild(this);
      Main.connections[id] = null;
      Main.connections[id] = undefined;
      Main.conlen = Main.connections.length; 
      Main.debug.df("Removed connection: "+str);
    }
  }
  
}